// templates/templates.js
Page({
  data: {
    activeTemplateId: null,
    scrollHeight: 500, // 初始高度
    templates: [
    
      {
        id: 1,
        name: "技术岗通用",
        description: "IT/互联网行业首选，突出技术栈与项目经验",
        preview: "/images/灰白色现代风应届生求职简历.png",
        component: "academicTemplate"
      },
      {
        id: 2,
        name: "金融精英",
        description: "银行/证券行业适用，强调财务分析与风险控制",
        preview: "/images/灰白色商务风现代个人介绍中文简历.png",
        component: "academicTemplate"
      },
      {
        id: 3,
        name: "市场营销",
        description: "突出市场策划与推广成果，适合快消行业",
        preview: "/images/蓝白色极简风求职简历.png",
        component: "creativeTemplate"
      },
      {
        id: 4,
        name: "管理培训生",
        description: "应届生专属，突出领导力与校园经历",
        preview: "/images/蓝白色商务风后端开发工程师应届生求职简历.png",
        component: "academicTemplate"
      },{
        id: 5,
        name: "高级经理",
        description: "10年+经验人士，强调团队管理与战略规划",
        preview: "/images/蓝白色极简大气风求职简历.png",
        component: "creativeTemplate"
      },{
        id: 6,
        name: "外语涉外",
        description: "外企/外贸岗位，突出语言能力与跨文化经验",
        preview: "/images/蓝白色极简大气风求职简历.png",
        component: "creativeTemplate"
      },{
        id: 7,
        name: "医疗护理",
        description: "医疗机构适用，突出临床经验与专业技能",
        preview: "/images/蓝白色极简大气风求职简历.png",
        component: "creativeTemplate"
      },{
        id: 8,
        name: "跨行业转型",
        description: "转行人士专用，突出可迁移能力与学习潜力",
        preview: "/images/蓝白色极简大气风求职简历.png",
        component: "creativeTemplate"
      }
    ]
  },

  onReady() {
    this.calculateScrollHeight();
  },

  calculateScrollHeight() {
    const systemInfo = wx.getSystemInfoSync();
    const query = this.createSelectorQuery();
    
    query.select('.page-title').boundingClientRect();
    query.select('.action-bar').boundingClientRect();
    query.exec(res => {
      const titleHeight = res[0].height;
      const barHeight = res[1].height;
      const padding = 20; // 上下padding总和
      
      this.setData({
        scrollHeight: systemInfo.windowHeight - titleHeight - barHeight - padding
      });
    });
  },

  handleSelectTemplate(e) {
    const templateId = e.currentTarget.dataset.id;
    this.setData({
      activeTemplateId: templateId === this.data.activeTemplateId ? null : templateId
    });
  },

  handleConfirm() {
    if (!this.data.activeTemplateId) {
      wx.showToast({
        title: '请先选择模板',
        icon: 'none'
      });
      return;
    }

    const selectedTemplate = this.data.templates.find(
      t => t.id === this.data.activeTemplateId
    );

    const app = getApp();
    const resumeData = app.globalData.resumeData || {};

    wx.navigateTo({
      url: `/pages/preview/preview`,
      success: (res) => {
        res.eventChannel.emit('sendTemplateData', {
          template: selectedTemplate,
          formData: resumeData
        });
      }
    });
  },

  onShow() {
    const app = getApp();
    if (app.globalData.selectedTemplateId) {
      this.setData({
        activeTemplateId: app.globalData.selectedTemplateId
      });
    }
  }
});